import { IalyLooger } from "../interface";

export default class Agent {
  constructor(logger: IalyLooger) {
    this.Url(logger);
    this.Browser(logger);
    this.OS(logger);
    this.SCR(logger);
  }

  Browser(logger: IalyLooger) {
    var browser_name = "";
    var Sys = {} as any;
    // var ua = navigator.userAgent.toLowerCase();
    /* var s;
    (s = ua.match(/msie ([\d.]+)/)) ? Sys.ie = s[1]:
      (s = ua.match(/firefox\/([\d.]+)/)) ? Sys.firefox = s[1] :
      (s = ua.match(/chrome\/([\d.]+)/)) ? Sys.chrome = s[1] :
      (s = ua.match(/opera.([\d.]+)/)) ? Sys.opera = s[1] :
      (s = ua.match(/version\/([\d.]+).*safari/)) ? Sys.safari = s[1] : 0; */
    if (Sys.ie) browser_name = 'IE: ' + Sys.ie;
    if (Sys.firefox) browser_name = 'Firefox: ' + Sys.firefox;
    if (Sys.chrome) browser_name = 'Chrome: ' + Sys.chrome;
    if (Sys.opera) browser_name = 'Opera: ' + Sys.opera;
    if (Sys.safari) browser_name = 'Safari: ' + Sys.safari;
    logger.push('_ua_', browser_name);
  }

  OS(logger: IalyLooger) {
    var os = "";
    if (navigator.platform.indexOf("Win") > -1) {
      os += 'win'
    } else if (navigator.platform.indexOf("Mac") > -1) {
      os += "Mac";
    } else if (navigator.platform.indexOf("X11") > -1) {
      os += "Unix";
    } else if (navigator.platform.indexOf("Linux") > -1) {
      os += "Linux";
    } else {
      os += "Other";
    }
    logger.push('_os_', os);
  }

  SCR(logger: IalyLooger) {
    logger.push("_scr_", window.screen.width + "*" + window.screen.height);
  }

  GetSID(c_name: string) {
    if (document.cookie.length > 0) {
      let c_start = document.cookie.indexOf(c_name + "=")
      if (c_start != -1) {
        c_start = c_start + c_name.length + 1
        let c_end = document.cookie.indexOf(";", c_start)
        if (c_end == -1) c_end = document.cookie.length
        return unescape(document.cookie.substring(c_start, c_end));
      }
    }
  }
  Url(logger: IalyLooger) {
    var title = document.title;
    logger.push('_title_', title);
    var ref = window.location.href;
    logger.push('_ref_', ref);
    logger.push('_url_', window.location.href);
    logger.push('_can_', document.cookie);
    logger.push('_createtime_', Date.now());
  }
}